package flc.ast.activity;

import android.text.TextUtils;
import android.view.View;

import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.bean.ResumeBean;
import flc.ast.databinding.ActivityInternshipExpBinding;
import flc.ast.dialog.SelDateDialog;
import flc.ast.dialog.TitleRenameDialog;
import flc.ast.utils.ResumeDataUtils;

public class InternshipExpActivity extends BaseAc<ActivityInternshipExpBinding> {

    public static int position;
    public static int modulePos;
    public static int dataPos;
    private boolean isAdd;
    private ResumeBean resumeBean;
    private ResumeBean.Module module;
    private ResumeBean.InternShipExperienceData internShipExperienceData;
    private ResumeBean.InternShipExperience internShipExperience;

    @Override
    protected int onCreate() {
        return R.layout.activity_internship_exp;
    }

    @Override
    protected void initView() {
        mDataBinding.ivInternshipExpBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.tvSave.setOnClickListener(this);
        mDataBinding.tvTitle.setOnClickListener(this);
        mDataBinding.llStartTime.setOnClickListener(this);
        mDataBinding.llEndTime.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        int id = v.getId();
        if (id == R.id.tvSave) {//保存
            save();
        } else if (id == R.id.tvTitle) {//标题
            showRenameDialog();
        } else if (id == R.id.llStartTime) {//入职时间
            showStartTimeDialog();
        } else if (id == R.id.llEndTime) {//离职时间
            showEndTimeDialog();
        }
    }

    @Override
    protected void initData() {
        resumeBean = ResumeDataUtils.getResumeList().get(position);
        module = resumeBean.getResumeData().get(modulePos);
        mDataBinding.tvTitle.setText(module.getAlias());
        if (module.getData() != null) {
            internShipExperienceData = GsonUtils.fromJson(module.getData().toString(), ResumeBean.InternShipExperienceData.class);
            if (dataPos == -1) {
                isAdd = true;
                internShipExperience = new ResumeBean.InternShipExperience();
                return;
            }
            internShipExperienceData.setInternShipExperienceList(GsonUtils.fromJson(internShipExperienceData.getInternShipExperienceList().toString(), new TypeToken<List<ResumeBean.InternShipExperience>>() {
            }.getType()));
            internShipExperience = internShipExperienceData.getInternShipExperienceList().get(dataPos);
            mDataBinding.etCompanyName.setText(internShipExperience.getCompanyName());
            mDataBinding.etJob.setText(internShipExperience.getJob());
            mDataBinding.tvStartTime.setText(internShipExperience.getStartTime());
            mDataBinding.tvEndTime.setText(internShipExperience.getEndTime());
            String detail = "";
            internShipExperience.setDetailList(GsonUtils.fromJson(internShipExperience.getDetailList().toString(), new TypeToken<List<ResumeBean.DetailItem>>() {
            }.getType()));
            for (ResumeBean.DetailItem detailItem : internShipExperience.getDetailList()) {
                detail += detailItem.getText() + "\n";
            }
            mDataBinding.etDetail.setText(detail);
        } else {
            isAdd = true;
            internShipExperienceData = new ResumeBean.InternShipExperienceData(new ArrayList<>());
            internShipExperience = new ResumeBean.InternShipExperience();
        }
    }

    private void save() {
        String companyName = mDataBinding.etCompanyName.getText().toString().trim();
        if (TextUtils.isEmpty(companyName)) {
            ToastUtils.showShort(getString(R.string.please_input_company_name));
            return;
        }
        String job = mDataBinding.etJob.getText().toString().trim();
        if (TextUtils.isEmpty(job)) {
            ToastUtils.showShort(getString(R.string.please_input_job));
            return;
        }
        String startTime = mDataBinding.tvStartTime.getText().toString().trim();
        if (TextUtils.isEmpty(startTime)) {
            ToastUtils.showShort(getString(R.string.please_sel_employment_date));
            return;
        }
        String endTime = mDataBinding.tvEndTime.getText().toString().trim();
        if (TextUtils.isEmpty(endTime)) {
            ToastUtils.showShort(getString(R.string.please_sel_resignation_date));
            return;
        }
        String detail = mDataBinding.etDetail.getText().toString().trim();
        if (TextUtils.isEmpty(detail)) {
            ToastUtils.showShort(getString(R.string.please_input_work_content));
            return;
        }

        String[] lines = detail.split("\\r?\\n");
        List<ResumeBean.DetailItem> list = new ArrayList<>();
        for (String line : lines) {
            list.add(new ResumeBean.DetailItem(line));
        }

        internShipExperience.setCompanyName(companyName);
        internShipExperience.setJob(job);
        internShipExperience.setStartTime(startTime);
        internShipExperience.setEndTime(endTime);
        internShipExperience.setDetailList(list);

        if (isAdd) {
            internShipExperienceData.getInternShipExperienceList().add(internShipExperience);
        }

        List<ResumeBean> listSave = ResumeDataUtils.getResumeList();
        module.setData(internShipExperienceData);
        listSave.set(position, resumeBean);
        ResumeDataUtils.saveResumeList(listSave);
        ToastUtils.showShort(getString(R.string.save_success));
        finish();
    }

    private void showRenameDialog() {
        TitleRenameDialog renameDialog = new TitleRenameDialog(mContext);
        renameDialog.text = mDataBinding.tvTitle.getText().toString().trim();
        renameDialog.setListener(new TitleRenameDialog.Ilistener() {

            @Override
            public void clickRename(String name) {
                module.setAlias(name);
                mDataBinding.tvTitle.setText(name);
            }
        });
        renameDialog.show();
    }

    private void showStartTimeDialog() {
        SelDateDialog dialog = new SelDateDialog(mContext);
        dialog.setListener(new SelDateDialog.Ilistener() {
            @Override
            public void clickConfirm(String date) {
                mDataBinding.tvStartTime.setText(date);
            }
        });
        dialog.show();
    }

    private void showEndTimeDialog() {
        SelDateDialog dialog = new SelDateDialog(mContext);
        dialog.setListener(new SelDateDialog.Ilistener() {
            @Override
            public void clickConfirm(String date) {
                mDataBinding.tvEndTime.setText(date);
            }
        });
        dialog.show();
    }

}